from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import mixins

# Create your views here.

from .models import UserFav,UserLeavingMessage,UserAddress
from .serializers import UserFavSerializer,UserFavDetaiaSerializer,LeavingMessageSerializer,AddressSerializer
from rest_framework.permissions import IsAuthenticated
from utils.permissions import IsOwnerOrReadOnly
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication


class UserFavViewset(viewsets.GenericViewSet,mixins.ListModelMixin,mixins.DestroyModelMixin,mixins.CreateModelMixin,mixins.RetrieveModelMixin):
    '''
    list:
        获取用户收藏列表
    retrieve:
        判断某个商品是否已经收藏
    create:
        收藏商品
    '''
    permission_classes = (IsAuthenticated,IsOwnerOrReadOnly)
    serializer_class = UserFavSerializer
    lookup_field = 'goods_id'
    authentication_classes = (JSONWebTokenAuthentication,SessionAuthentication)

    # 收藏+1
    def get_queryset(self):
        if self.action =='list':
            return UserFavDetaiaSerializer
        elif self.action == 'create':
            return UserFavSerializer

        return UserFavSerializer


class LeavingMessageViewset(mixins.ListModelMixin,mixins.DestroyModelMixin,mixins.CreateModelMixin,viewsets.GenericViewSet):
    '''
    list:
        获取用户留言
    create：
        添加留言
    delete：
        删除留言功能
    '''
    permission_classes = (IsAuthenticated,IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication,SessionAuthentication)
    serializer_class = LeavingMessageSerializer

    # 只能看到自己的留言
    def get_queryset(self):
        return UserLeavingMessage.objects.filter(user=self.request.user)


class AddressViewset(viewsets.ModelViewSet):
    '''
    收货地址管理
    list:
        获取收货地址
    create:
        添加收货地址
    update：
        更新收货地址
    delete：
        删除收货地址

    '''
    permission_classes = (IsAuthenticated,IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication,SessionAuthentication)
    serializer_class = AddressSerializer

    def get_queryset(self):
       return UserAddress.objects.filter(user=self.request.user)

